package com.xtm.exercise.utils.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.one.common.utils.easyexcel.model.EasyExcelImportResult;
import com.one.common.utils.service.IEasyExcelImportService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

public class EasyExcelUtil {

    private static final Integer BATCH_EXPORT_SIZE = 50000;
    private static final Integer BATCH_IMPORT_SIZE = 200;
    private static Logger logger = LoggerFactory.getLogger(EasyExcelUtil.class);

    private EasyExcelUtil(){}

    /**
     *
     * @param fileInputStream   导入文件流
     * @param modelClass        Excel与实体映射
     * @param easyExcelImportService    处理数据验证和最终保存的service
     * @param headRowNumber     //标识文件头从第几行开始，可以不填，不填的话默认从第一行
     * @return
     */
    public static EasyExcelImportResult readFromExcel(InputStream fileInputStream , Class modelClass , IEasyExcelImportService easyExcelImportService , Map<String , Object> otherParamsMap , AsyncTaskExecutor asyncTaskExecutor , Integer... headRowNumber){
        EasyExcelImportResult importResult = new EasyExcelImportResult();
        Integer headRowNum = 1;
        if(headRowNumber != null && headRowNumber.length > 0){
            headRowNum = headRowNumber[0];
        }
        EasyExcel.read(fileInputStream , modelClass , new ImportDataListener(easyExcelImportService , importResult , otherParamsMap , asyncTaskExecutor , headRowNum)).sheet().headRowNumber(headRowNum).doRead();
        return importResult;
    }

    /**
     * 读取Excel（一个sheet）
     * @param excel 文件
     * @param clazz 实体类
     * @param sheetNo sheet序号
     * @return 返回实体列表(需转换)
     */
    public static <T> List<T> readExcel(MultipartFile excel, Class<T> clazz, int sheetNo) {

        EasyExcelListener excelListener = new EasyExcelListener();

        ExcelReader excelReader = getReader(excel,clazz,excelListener);
        if (excelReader == null) {
            return new ArrayList<>();
        }

        ReadSheet readSheet = EasyExcel.readSheet(sheetNo).build();

        excelReader.read(readSheet);
        excelReader.finish();

        return BeanConvert.objectConvertBean(excelListener.getDataList(), clazz);
    }


    /**
     * 读取Excel（多个sheet可以用同一个实体类解析）
     * @param excel 文件
     * @param clazz 实体类
     * @return 返回实体列表(需转换)
     */
    public static <T> List<T> readExcel(MultipartFile excel, Class<T> clazz) {

        EasyExcelListener excelListener = new EasyExcelListener();
        ExcelReader excelReader = getReader(excel,clazz,excelListener);

        if (excelReader == null) {
            return new ArrayList<>();
        }

        List<ReadSheet> readSheetList = excelReader.excelExecutor().sheetList();

        for (ReadSheet readSheet:readSheetList){
            excelReader.read(readSheet);
        }
        excelReader.finish();

        return BeanConvert.objectConvertBean(excelListener.getDataList(), clazz);
    }


    /**
     * 导出Excel(一个sheet)
     *
     * @param response  HttpServletResponse
     * @param list      数据list
     * @param fileName  导出的文件名
     * @param sheetName 导入文件的sheet名
     * @param clazz 实体类
     */
    public static <T> void  writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> clazz) {

        OutputStream outputStream = getOutputStream(response, fileName);

        ExcelWriter excelWriter = EasyExcel.write(outputStream, clazz).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();

        excelWriter.write(list, writeSheet);

        excelWriter.finish();
    }


    /**
     * 导出Excel(带样式)
     *
     *
     * 中文	英文
     * 她在房间里唱歌。	She sings in the room.
     * 他喜欢那首歌吗？	Does he like the song?
     * 是的，他喜欢。	Yes, he likes it.
     * 我喜欢那辆蓝色的公交车。	I like that blue bus.
     * 你喜欢那辆蓝色的公交车吗？	Do you like that blue bus?
     * 是的，我喜欢。	Yes, I like it.或是（Yes, I do.）
     * 为什么你喜欢那辆蓝色的公交车？	Why do you like that blue bus?
     * 我喜欢那辆蓝色的公交车因为它是漂亮的。	I like that blue bus because it is beautiful.
     * 我喜欢那个花园。	I like that garden.
     * 你喜欢那个花园吗？	Do you like that garden?
     * 是的，我喜欢。	Yes, I like it.或是（Yes, I do.）
     * 我喜欢那个花园因为有许多花在那个花园里。	I like that garden because there are many flowers in that garden.
     * 他喜欢在公园里跑步。	He likes to run in the park.
     * 他喜欢跑步。	He likes to run.
     * 他喜欢阅读那本书。	He likes to read that book.
     * 他喜欢这件事。＝他喜欢跑步（这件事）。	He likes it.＝He likes to run.
     * 他喜欢这件事。＝他喜欢读那本书（这件事）。	He likes it.＝He likes to read that book.
     * 他喜欢在这个公园里跑步。	He likes to run in this park.
     * 我喜欢散步。	I like to walk.
     * 我喜欢在那个公园里散步。	I like to walk in the (that) park.
     * 他喜欢跑步。	He likes to run .
     * 他喜欢在这个森林里跑步。	He likes to run in the (this) forest.
     * 她喜欢游泳。	She likes to swim.
     * 她喜欢在那个游泳池里游泳。	She likes to swim in the (that) pool.
     * 他们喜欢玩耍。	They like to play.
     * 他们喜欢在我的学校里玩耍。	They like to play in my school.
     * 约翰的父亲喜欢教我们英文。	John’s father likes to teach us English.
     * 约翰的父亲喜欢在他的客厅里教我们英文。	John’s father likes to teach us English in his living room.
     * 他喜欢阅读而且他喜欢跑步。	He likes to read and he likes to run.
     * 他喜欢阅读和跑步。	He likes to read and run.
     * 他喜欢散步而且他喜欢游泳。	He likes to walk and he likes to swim.
     * 他喜欢散步和游泳。	He likes to walk and swim.
     * 我喜欢唱歌而且我喜欢开车。	I like to sing and I like to drive.
     * 我喜欢唱歌和开车。	I like to sing and drive.
     * 你喜欢学习而且你喜欢工作。	You like to learn and you like to work.
     * 你喜欢学习和工作。	You like to learn and work.
     * 那些猫喜欢吃而且那些猫喜欢睡。	Those cats like to eat and those cats like to sleep.
     * 那些猫喜欢吃和睡。	Those cats like to eat and sleep.
     * 我的父亲(是)总是在他的办公室。	My father is always in his office.
     * 他总是忙碌的。	He is always busy.
     * 你是常常忙碌的。	You are often busy.
     * 他通常在公园里跑步	He usually runs in the park
     * 他很少在森林里玩。	He seldom plays in the forest.
     * 那只猴子有时候在花园里玩。	That monkey sometimes plays in the garden.
     * 她的房子（是）总是脏的。	Her house is always dirty.
     * 她的钢琴（是）常常脏的。	Her piano is often dirty.
     * 这个年轻人（是）通常虚弱的。	The (This) young person is usually weak.
     * 那个邮差（是）有时候忙碌的。	The (That) postman is sometimes busy.
     * 他们的父母（是）很少仁慈的。	Their parents are seldom kind.
     * 虽然那位绅士是富有的，他（是）从不懒惰的。	Although that gentleman is rich, he is never lazy.
     * 他们（是）经常伤心的因为他们是常常生病的。	They are usually sad because they are often sick.
     * 他女儿总是在房间里玩。	His daughter always plays in the room.
     * 玛丽从不微笑。	Mary never smiles.
     * 我妈妈很少在客厅里喝酒。	My mother seldom drinks in the living room.
     * 那个老师经常在办公室里画画。	That teacher usually draws in the office.
     * 你父亲有时候在这个泳池游泳。	Your father sometimes swims in the (this) pool.
     * 他常常教我英文。	He often teaches me English.
     * 她有时教我英文。	She sometimes teaches me English.
     * 有时她教我英文	Sometimes she teaches me English.
     * 那个学生有时是懒惰的。	That student is sometimes lazy.
     * 有时那个学生是懒惰的。	Sometimes that student is lazy.
     * 那个老师有时教我们英文。	That teacher sometimes teaches us English.
     * 有时那个老师教我们英文。	Sometimes that teacher teaches us English.
     * 那只狗跑得快	That dog runs quickly.
     * 他跑得快。	He runs quickly.
     * 他跑得慢。	He runs slowly.
     * 你妹妹跑得快。	Your sister runs quickly.
     * 你妹妹跑得快吗？	Does your sister run quickly?
     * 我哥哥走得慢。	My brother walks slowly.
     * 我哥哥走得慢吗？	Does my brother walk slowly?
     * 她伤心地唱歌。	She sings sadly.
     * 她伤心地唱歌吗？	Does she sing sadly?
     * 那个女孩打扮得很漂亮。（dress第三人称单数＝dresses）	That girl dresses beautifully.
     * 那个女孩打扮得很漂亮吗？	Does that girl dress beautifully?
     * 那个学生快乐地学习。	That student learns happily.
     * 那个学生快乐地学习吗？	Does that student learn happily?
     * 那个老人饥饿地工作。	That old man works hungrily
     * 那个老人饥饿地工作吗？	Does that old man work hungrily?
     * 那位司机忙碌地开车。	That driver drives busily.
     * 那位司机忙碌地开车吗？	Does that driver drive busily?
     * 那个女孩生气地哭泣。	That girl cries angrily.
     * 那个女孩生气地哭泣吗？	Does that girl cry angrily?
     * 那只鸟飞得快。	That bird flies fast.
     * 为什么那只鸟飞得快？	Why does that bird fly fast?
     * 那个农夫早工作。	That farmer works early.
     * 为什么那个农夫早工作？	Why does that farmer work early?
     * 那位男孩晚到达。	That boy arrives late.
     * 为什么那个男孩晚到达？	Why does that boy arrive late?
     * 你的朋友努力地工作。	Your friend works hard.
     * 为什么你的朋友努力地工作？	Why does your friend work hard?
     * 他数学教得好。	He teaches math well.
     * 我妹妹唱得好。	My sister sings well.
     * 为什么我妹妹唱得好？	Why does my sister sing well?
     * 那个老师英文教得好。	That teacher teaches English well.
     * 为什么那个老师英文教得好？	Why does that teacher teach English well?
     * 这个学生画得差。	This student draws badly.
     * 为什么这个学生画得差？	Why does this student draw badly?
     * 她今天在这里工作。	She works here today.
     * 我在这里	I am here.
     * 我在那里教他英文。	I teach him English there.
     * 她常常来这里。	She often comes here.
     * 我有时去那里。	＝I sometimes go there.（O）
     * ＝Sometimes I go there.（O）
     * 她常常来这里。	She often comes here.
     * 她常常来这里吗？	Does she often come here?
     * 为什么她常常来这里？	Why does she often come here?
     * 她常常来这里因为她喜欢这里（likes it）。	She often comes here because she likes it here.
     * 他总是去那里。	He always goes there.
     * 他总是去那里吗？	Does he always go there?
     * 为什么他总是去那里？	Why does he always go there?
     * 他总是去那里因为他不喜欢来这里。	He always goes there because he doesn’t like to come here.
     * 那个老师现在是生气的。	That teacher is angry now.
     * 我们今天学历史。	We learn history today.
     * 你来得早。	You come early.
     * 我们今天学英文。	We learn English today.
     * 我们今天学英文吗？	Do we learn English today?
     * 我们今天何时学英文？	When do we learn English today?
     * 我们今晚唱歌。	We sing tonight.
     * 我们今晚唱歌吗？	Do we sing tonight?
     * 我们今晚何时唱歌？	When do we sing tonight?
     * 他现在是虚弱的。	He is weak now.
     * 他现在是虚弱的吗？	Is he weak now?
     * 为什么他现在是虚弱的？	Why is he weak now?
     * 他来得早。	He comes early.
     * 他来得早吗？	Does he come early?
     * 为什么他来得早？	Why does he come early?
     * 她来得晚。	She comes late.
     * 她来得晚吗？	Does she come late?
     * 为什么她来得晚？	Why does she come late?
     * 他们现在在这里。	They are here now.
     * 你们今天在这里学历史。	You learn history here today.
     * 我们今晚在这里唱歌。	We sing here tonight.
     * 我们今天在那里学英文。	We learn English there today.
     * 她现在在这里。	She is here now.
     * 他早去那里。	He goes there early.
     * 她晚来这里。	She comes here late.
     * 她唱得非常好而且她是非常漂亮的。	She sings very well and she is very beautiful.
     * 他非常喜欢我。	He likes me very much.
     * 他唱得非常好。	He sings very well.
     * 这架钢琴是非常好的，但它很旧。	This piano is very good , but it is very old.
     * 我哥哥是非常聪明的，但他很自私。	My brother is very smart, but he is selfish.
     * 那个农夫是非常笨的，但他很体贴	That farmer is very stupid, but he is nice.
     * 虽然这只狗非常小，它跑得很快	Although this dog is very small, it runs very fast (quickly).
     * 那个学生是非常聪明的，因为他学得很快	That student is very smart because he learns very quickly.
     * 我非常喜欢那个女孩，因为她很可爱	I like the(that) girl very much because she is very cute.
     * 我非常喜欢那只鸟，因为它唱得很好。	I like that bird very much because it sings very well.
     * 他也是一位学生。	He is also a student.
     * He is a student too.
     * 你也喜欢游泳吗？	　　＝Do you also like to swim?（O）（also习惯放在实义动词前面。）
     * 　　＝Do you like to swim too?（O）（too习惯放在句尾。）
     * 我不是学生，而且他也不是学生。	I am not a student, and he is not a student either.
     * 他也不喜欢开车。	　　＝He also doesn’t like to drive.（O）（also可用于否定句和肯定句。）
     * 　　＝He doesn’t like to drive either.（O）（either只能用于否定句。）
     * 我妈妈也是非常生气的。	My mother is also very angry. (My mother is very angry too.)
     * 你妈妈也是非常生气的吗？	Is your mother also very angry? (Is your mother very angry too?)
     * 他也有一台收音机。	He also has a radio. (He has a radio too.)
     * 他也有一台收音机吗？	Does he also have a radio? (Does he have a radio too?)
     * 我也喜欢在这里唱歌。	I also like to sing here. (I like to sing here too.)
     * 你也喜欢在这里唱歌吗？	Do you also like to sing here? (Do you like to sing here too?)
     * 我也在那里教美术。	I also teach art there. (I teach art there too.)
     * 你也在那里教美术吗？	Do you also teach art there? ( Do you teach art there too?)
     * 我姐姐也不喜欢看电视。	My sister also doesn’t like to watch TV. (My sister doesn’t like to watch TV either.)
     * 他们也不喜欢在那个公园里玩。	They also don’t like to play in the(that) park. (They don’t like to play in the (that) park either.)
     * 今天大约有3个学生在这里。	There are about three students here today.
     * 我大约有5个苹果。	I have about five apples.
     * 有多少鸟在我的书桌上？	How many birds are there on my desk?
     * 大约有10只鸟在你的书桌上。	There are about ten birds on your desk.
     * 你有多少手表？	How many watches do you have?
     * 我大约有7块手表。	I have about seven watches.
     * 有多少学生在那个学校？	How many students are there in the (that) school?
     * 大约有360个学生在那个学校。	There are about three hundred and sixty students in the (that) school.
     * 你教多少学生？	How many students do you teach?
     * 我在那里教大约40个学生。	I teach about forty students there.
     * 房间里只有3个人。	There are only three persons in the room.
     * 我只有2支笔。	I only have two pens.
     * 今天教室里只有3个人。	There are only three persons in the classroom today.
     * 这里现在只有两个小孩。	There are only two children here now.
     * 我只有一个棒球。	I only have a baseball.
     * 他只喜欢在大海（ocean）里游泳。	He only likes to swim in the ocean.
     * 这个小孩现在只想要那个玩具。	The (This) child only wants that toy now.
     * 那些孩子经常跑到森林。	Those children usually run to the forest.
     * 他跑向那个公园。	He runs to that park.
     * 他跑向那里。	He runs there.
     * 他常去那个书店。	He often goes to the bookstore.
     * 他总是很早去上学。	He always goes to school early.
     * 他总很晚去睡觉。	He always goes to bed late.
     * 有时候他很晚回家。	Sometimes he goes home late.
     * 他很少来这里。	He seldom comes here.
     * 那些狗常跑向公园。	Those dogs often run to the park.
     * 为什么那些狗常跑向公园？	Why do those dogs often run to the park?
     * 它们常跑向公园因为它们喜欢那里	They often run to the park because they like it there.
     * 我常常去车站。	I often go to the station.
     * 为什么你常常去车站？	Why do you often go to the station?
     * 我常去车站因为我母亲在那里工作。	I often go to the station because my mother works there.
     * 那个胖司机很少来我的餐厅	That fat driver seldom comes to my restaurant.
     * 为什么那个胖司机很少去你的餐厅？	Why does that fat driver seldom go to your restaurant?
     * 他很少来我的餐厅因为我的餐厅是非常小的。	He seldom comes to my restaurant because my restaurant is very small.
     * 那个医生经常很晚回家。	That doctor usually goes home late.
     * 我弟弟总是很早上床睡觉。	My brother always goes to bed early.
     * 那个学生总是很早上学。	That student always goes to school early.
     * 我和他跑步。	I run with him.
     * 我常和他游泳。	I often swim with him.
     * 为什么你常和他游泳？	Why do you often swim with him?
     * 因为他游得非常快，我想要学习他的技巧（skill）。	Because he swims very fast (quickly), I want to learn his skill.
     * 我很少和他喝酒。	I seldom drink with him.
     * 为什么你很少和他喝酒？	Why do you seldom drink with him?
     * 我很少和他喝酒因为他总是喜欢大声地唱歌。	I seldom drink with him because he always likes to sing loudly.
     * 这些书是给你的。	These books are for you.
     * 我教他数学。	I teach math for him
     * 这些花是给你的。	These flowers are for you.
     * 这些花是给我的吗？	Are these flowers for me?
     * 是的，它们是。	Yes, they are.
     * 为什么这些花是给我的？	Why are these flowers for me?
     * 因为我喜欢你。	Because I like you.
     * 那本英文书是给我的吗？	Is that English book for me?
     * 是的，它是。	Yes, it is.
     * 为什么那本英文书是给我的？	Why is that English book for me?
     * 因为我不想教你英文了。	Because I don’t want to teach English for you.
     * 如果你想要说好英语，你必须努力学习。	If you want to speak English well, you have to study hard.
     * 如果你想要睡觉，你必须回家。	If you want to go to bed, you have to go home.
     * If you want to sleep, you have to go home.
     * 你必须努力学习，如果你想要说好英语。	You have to study hard if you want to speak English well.
     * 为什么我妹妹英语说得非常差。	Why does my sister speak English very badly?
     * 她英语说得非常差因为她是非常懒惰的。	She speaks English very badly because she is very lazy.
     * 如果你妹妹想要说好英语，她必须努力学习。	If your sister wants to speak English well, she has to study hard.
     * 我想要和约翰在客厅里玩。	I want to play with John in the living room.
     * 如果你想和约翰玩，你现在必须打扫你的卧室。	If you want to play with John, you have to clean your bedroom.
     * 你教得好吗？	Do you teach well?
     * 是的，我教得非常好。	Yes, I teach very well.
     * 如果你想要学得快，你必须努力学习。	If you want to learn quickly, you have to study hard.
     * 每天去跑步是好的。	It is good to run every day.
     * 上学对我来说是快乐的。	It is happy for me to go to school.
     * 每天努力学习是好的。	It is good to study hard every day.
     * 每天喝酒是不好的	It is bad to drink every day.
     * 每天抽烟是不好的	It is bad to smoke every day.
     * 对我来说每天去书店是快乐的。	It is happy for me to go to the bookstore every day.
     * 对你爸爸来说每天看电视是快乐的。	It is happy for your father to watch TV every day.
     * 对她来说和约翰游泳是快乐的。	It is happy for her to swim with John.
     * 如果你想要学得好，每天努力学习是非常重要的（important）。	If you want to learn well, it is very important to study hard every day.
     * 如果你想跑得快，每天练习是非常重要的	If you want to run quickly, it is very important to practice every day.
     * 她必须在10点学习英文。	She has to learn English at ten o’clock.
     * “1点钟～12点钟”的写法
     * 几点了？	What time is it?
     * 现在是两点钟了。	It is two o’clock now.
     * 现在是8点了。	It is eight o’clock now.
     * 现在是8点了吗？	Is it eight o’clock now?
     * 是的，现在是8点了。	Yes, it is eight o’clock now.
     * 为什么你非常生气？	Why are you very angry?
     * 我很生气因为我的时钟是坏的	I am very angry because my clock is broken.
     * 在9点我有一个重要的考试，所以我必须吃快点。	I have an important test at nine o’clock, so I have to eat quickly.
     * 我也必须开得很快如果我想要在9点抵达那里	I also have to drive very fast (quickly) if I want to arrive there at nine o’clock.
     * “周一～周日”的写法
     * 约翰打算在周五和比利游泳。	Jonn plans to swim with Billy on Friday.
     * 何时是玛丽的生日?	When is Mary’s birthday?
     * 玛丽的生日是在星期六。	Mary’s birthday is on Staurday.
     * 我想要在星期六庆祝玛丽的生日。	I want to celebrate Mary’s birthday on Saturday.
     * 对我来说庆祝玛丽的生日是非常重要的	It is very important for me to celebrate Mary’s birthday.
     * 如果你想要庆祝玛丽的生日，你必须买一个蛋糕。	If you want to celebrate Mary’s birthday, you have to buy a cake.
     * 星期五我想要在那家蛋糕店（at the cake store）买一个蛋糕。	I want to buy a cake at the cake store on Friday.
     * 那家蛋糕店在哪里？	Where is the(that) cake store?
     * 那家蛋糕店在书店附近（near）。	The (That) cake store is near the bookstore.
     * 星期五你想要和我去买个蛋糕吗？	Do you want to buy a cake with me on Friday?
     * 虽然星期五我有个重要的考试，但我想要和你买蛋糕。	Although I have an important test on Friday, I want to buy a cake with you.
     * “1月～12月”的写法
     * 我想要在2月去日本。	I want to go to Japan in February
     * 在台湾7月天气是非常热的。	It is very hot in Taiwan in July.
     * 你何时来台湾？	When do you come to Taiwan?
     * 我打算在10月去台湾。	I plan to go to Taiwan in October.
     * 10月有许多游客在台湾。	There are many tourists in Taiwan in October.
     * 为什么在10月有许多游客在台湾？	Why are there many tourists in Taiwan in October?
     * 因为在10月台湾天气是非常凉爽的	Because it is very cool in Taiwan in October.
     * 在7月台湾也有许多美国人吗？	Are there also many Americans in Taiwan in July?
     * 是的，7月也有许多美国人在台湾。	Yes, there are also many Americans in Taiwan in July.
     * 今晚天气是非常热的。	It is very hot tonight.
     * 今天早上我不想上学。	I don’t want to go to school this morning.
     * 今天是我的生日	It is my birthday today.
     * Today is my birthday.
     * 今晚天气很冷。	It is very cold tonight.
     * Tonight is very cold.
     * 我喜欢每天上学。	I like to go to school every day.
     * 你也喜欢每天上学吗？	Do you also like to go to school every day?
     * 不，我讨厌每天上学。	No, I hate to go to school every day.
     * 因为学校的那只狗总是喜欢咬我。	Because the dog in the school always likes to bite me.
     * 如果你想要和玛丽游泳，今天早上你必须完成这个工作。	If you want to swim with Mary, you have to finish the (this) work this morning.
     * 她像他一样高，而且她比我高。	She is as tall as he,and she is taller than I.
     * 你像他一样聪明。	You are as smart as he.
     * 她跑步像我一样快。	She runs as quickly as I.
     * 她像我一样开心。	She is as happy as I.
     * 为什么她像你一样开心呢？	Why is she as happy as you?
     * 她像我一样开心因为我们今天要去动物园。	She is as happy as I because we want to go to the zoo today.
     * 为什么你弟弟像她妹妹一样伤心呢？	Why is your brother as sad as her sister?
     * 我弟弟像她妹妹一样伤心因为这个早上他们必须学英文。	My brother is as sad as her sister because they have to learn English this morning.
     * 你学英文（学得）好吗？	Do you learn English well?
     * 是的，我学英文（学得）非常好。	Yes, I learn English very well.
     * 我英文学得像她一样好。	I learn English as well as she.
     * 我比他高	I am taller＋than he.
     * 你比他可爱。	You are cuter than he.
     * 你工作比我努力。	You work harder than I.
     * 他和你一样聪明吗？	Is he as smart as you?
     * 不，他比我聪明。	No, he is smarter than I.
     * 虽然他比你聪明，你工作比他努力。	Although he is smarter than you, you work harder than he.
     * 因为我工作比他努力，我比他富有。	Because I work harder than he, I am richer than he.
     * 你的女儿比他的女儿漂亮吗？	Is your daughter more beautiful than his daughter?
     * 是的，我的女儿比他的女儿漂亮。	Yes, my daughter is more beautiful than his daughter.
     * 你比那个医生快乐吗？	Are you happier than that doctor?
     * 是的，我比那个医生快乐。	Yes, I am happier than that doctor.
     * 我是班上最高的	I am the tallest in the class.
     * 我们的教室是这所学校里最干净的。	Our classroom is the cleanest in the (this) school.
     * 他是班级里跑最快的。	He runs the fastest in the class.
     * He runs fastest in the class.
     * Our classroom is the cleanest in the school.
     * 那只狐狸是森林里跑最快的。	That fox runs the fastest (the most quickly) in the forest.
     * 为什么那只狐狸是森林里跑最快的？	Why does that fox run the fastest (the most quickly) in the forest?
     * 因为它跑得比那只老虎还快。	Because it runs faster(more quickly) than that tiger.
     * 这朵花是森林里最漂亮的花。	The flower is the most beautiful flower in the forest.
     * 为什么这朵花是森林里最漂亮的花？	Why is the flower the most beautiful flower in the forest?
     * 因为这朵花跟你一样漂亮。	Because this flower is as beautiful as you.
     * 那个农夫是镇上最富有的人。	That farmer is the richest person in the town.
     * 为什么那个农夫是镇上最富有的人？	Why is that farmer the richest person in the town?
     * 因为那个农夫工作跟我一样努力	Because that farmer works as hard as I.
     * 我比较喜欢那张桌子。	I like that table more
     * 我喜欢那本书多过这本书。	I like that book more than this book.
     * 我最喜欢那张桌子。	I like that table the most
     * I like that table most
     * 这个棒球选手是学校里最好的	This baseball player is the best in the school.
     * 他也是学校里最高大的学生。	He is also the biggest student in the school.
     * 那个老师是学校里最差的	That teacher is the worst in the school.
     * 你更喜欢游泳吗？	Do you like to swim more?
     * 我喜欢游泳多于我喜欢跑步	I like to swim more than I like to run.
     * 我最喜欢游泳了	I like to swim most.
     * 他现在正在工作吗？	Is he working now?
     * 不，他现在正在睡觉。	No, he is sleeping now.
     * 玛丽正在工作。	Mary is working.
     * 你的狗现在正在睡觉。	Your dog is sleeping now.
     * 你在哪里？	Where are you?
     * 我在公园里。	I am in the park.
     * 我正在公园里散步	I am walking in the park.
     * 对我来说每天散步是非常重要的。	It is very important for me to walk every day.
     * 有许多人现在正在公园里散步。	There are many persons walking in the park now.
     * 你想要来这里吗？	Do you want to come here?
     * 不，我正在客厅里看电视。	No, I am watching TV in the living room.
     * 我喜欢看电视多于我喜欢散步。	I like to watch TV more than I like to walk.
     * 你现在正在做什么呢?	What are you doing now?
     * 我正在写字。	I am writing.
     * 为什么你正在写字呢？	Why are you writing?
     * 因为我正在做我的作业	Because I am doing my homework.
     * 比利在哪里？	Where is Billy?
     * 他正在和他的狗玩。	He is playing with his dog.
     * 他们正在挖花园里的土吗？（泥土dirt不可数名词；the dirt in the garden）	Are they digging the dirt in the garden?
     * 不，他们正在森林里玩。	No, they are playing in the forest.
     * 他们正在玩什么？	What are they playing?
     * 他们正在跑步。	They are running.
     * 谁跑得比较快呢？	Who is faster?
     * 比利跑得比那只胖狗快。	Billy runs faster than that fat dog.
     * 你也跑得像比利一样快吗？	Do you also run as fast as Billy?
     * 不，我跑得像那只胖狗一样慢。	No, I run as slowly as that fat dog.
     * 你现在在做什么？	What are you doing now?
     * 我正在公园里跑步。	I am running in the park.
     * 他正在洗他的自行车吗？	Is he washing his bicycle?
     * 不，他已经洗完了他的自行车。	No, he has washed his bicycle.
     * 我已经打开了门。	I have opened the door.
     * 她已经买了两把椅子。	She has bought two chairs.
     * 我现在非常饿。	I am very hungry now.
     * 你想要吃一个鸡蛋吗？	Do you want to eat an egg?
     * 我已经吃9个鸡蛋了。	I have eaten nine eggs.
     * 你想要吃一些面包吗？	Do you want to eat some bread?
     * 我已经吃很多面包了。	I have eaten much bread.
     * 你想要喝一些果汁吗？	Do you want to drink some juice?
     * 我已经喝许多果汁了。	I have drunk much juice.
     * 你必须现在去睡觉因为你已经吃了许多东西了。	You have to sleep now because you have eaten many things.
     * 你已经买了一个蛋糕了吗？	Have you bought a cake?
     * 你想要买那本最新的小说吗？	Do you want to buy the newest novel?
     * 我已经买了那本最新的小说了。	I have bought the newest novel.
     * 你已经读了那本小说了吗？	Have you read that novel?
     * 不，我还没有读那本小说。	No, I have not read that novel.
     * 你现在正在做什么？	What are you doing now?
     * 我现在正在洗我爸爸的车。	I am washing my father’s car now
     * 你已经完成了你的作业了吗？	Have you finished your homework?
     * 是的，我已经完成了我的作业。	Yes, I have finished my homework.
     * 我还没完成我的作业因为我正在读那本小说。	I have not finished my homework because I am reading that novel.
     * 玛丽已经去日本了。	Mary has gone to Japan.
     * 玛丽已经去过日本了。	Mary has been to Japan.
     * 你曾经去过美国吗？	Have you ever been to America?
     * 你曾经去过韩国吗？	Have you ever been to Korea?
     * 不，我还没去过韩国。	No, I have not been to Korea.
     * 你已经去过哪一个国家呢？	Which country have you been to?
     * 我已经去过泰国、日本和美国。	I have been to Thailand, Japan and America.
     * 今天我弟弟已经去泰国了。	My brother has gone to Tailand today.
     * 他喜欢泰国因为那里有许多好吃的海鲜	He likes Tailand because there is much delicious seafood there.
     * 我计划这个月去英国	I plan to go to England this month.
     * 你曾经去过英国吗？	Have you ever been to England?
     * 是的，我已经去过英国了。	Yes, I have been to England.
     * 我喜欢英国因为英国是一个漂亮的国家。	I like England because England is a beautiful country.
     * 我已经持续工作3小时了。	I have been working for three hours.
     * 他已经洗完车了吗？	Has he washed the car?
     * 不，他还没有洗完车。	No, he has not washed the car.
     * 他已经持续洗车2小时了。	He has been washing the car for two hours.
     * 为什么他已经持续洗车2小时了？	Why has he been washing the car for two hours?
     * 他已经持续洗车2小时因为那辆车非常脏。	He has been washing the car for two hours because that car is very dirty.
     * 你的女儿正在哭吗？	Is your daughter crying?
     * 是的，她已经持续哭40分钟了。	Yes, she has been crying for forty minutes.
     * 为什么她已经持续哭泣40分钟呢？	Why has she been crying for forty minutes?
     * 因为她还没有完成她的作业	Because she has not finished her homework.
     * 从2点开始我就已经持续地游泳。	I have been swimming since two o’clock.
     * 他已经完成了他的作业了吗？	Has he finished his homework?
     * 不，他还没有完成他的作业。	No, he has not finished his homework.
     * 从3点开始他就已经持续地睡觉。	He has been sleeping since three o’clock.
     * 他已经持续睡了4小时了。	He has been sleeping for four hours.
     * 为什么从3点开始他就已经持续地睡觉？	Why has he been sleeping since three o’clock?
     * 因为今天他非常累。	Because he is very tired today.
     * 今天下午他已经持续走了1小时。	He has been walking for one hour this afternoon.
     * 为什么今天下午他已经持续走了1小时？	Why has he been walking for one hour this afternoon?
     * 因为他的自行车坏了。	Because his bicycle is broken.
     * 他昨天在那个公园玩。	He played in the park yesterday.
     * 他3天之前去了那个动物园。	He went to the zoo three days ago.
     * 他昨天在那个森林跑步吗？	Did he run in the forest yesterday
     * 不，他昨天没有在那个森林跑步。	No, he didn’t run in the forest yesterday.
     * 我以前是一位医生。	I was a doctor before.
     * 你以前是一位医生吗？	Were you a doctor before?
     * 是的，我以前是一位医生。	Yes, I was a doctor before.
     * 因为我喜欢教英文，我现在是一位英文老师。	Because I like to teach English, I am an English teacher now.
     * 你何时在这个城市（city）里教英文呢？	When did you teach English in this city?
     * 去年我开始在这个城市里教英文。	I started to teach English in this city last year.
     * 你以前在哪里教英文？	Where did you teach English before?
     * 我两年之前（two years ago）在台北教英语。	I taught English in Taipei two years ago.
     * 台北也是一个漂亮的城市。	Taipei is also a beautiful city.
     * 昨天下午3点他正在睡觉。	He was sleeping at three o’clock yesterday afternoon.
     * 当他昨晚来这里时,我正在唱歌。	When he came here last night, I was singing.
     * 你昨天早上（yesterday morning）9点正在做什么？	What were you doing at nine o’clock yesterday morning?
     * 我昨天早上9点正在海里游泳	I was swimming in the ocean at nine o’clock yesterday morning.
     * 昨天早上有多少人在海里呢？	How many persons were there in the ocean yesterday morning?
     * 昨天早上有大约25个人在海里。	There were about twenty-five persons in the ocean yesterday morning.
     * 昨天当我正在游泳时，你正在做什么？	When I was swimming yesterday, what were you doing? (What were you doing when I was swimming yesterday?)
     * 当你正在游泳时，我正在做我的作业。	When you were swimming, I was doing my homework.
     * 昨天下午5点他正在做什么？	What was he doing at five o’clock yesterday afternoon?
     * 昨天下午5点他正在客厅看电视。	He was watching TV in the living room at five o’clcok yesterday afternoon.
     * 他已经洗完车子了吗？	Has he washed the car?
     * 是的，他昨天已经洗完车子了。	Yes, he had wased the car yesterday.
     * 玛丽昨天已经去日本了吗？	Had Mary gone to Japan yesterday?
     * 是的，她昨天已经去日本了。	Yes, she had gone to Japan yesterday.
     * 她何时抵达日本的？	When did she arrive in Japan?
     * 她昨天下午2点抵达日本。	She arrived in Japan at two o’clock yesterday afternoon.
     * 比利昨天也已经抵达日本了吗？	Had Billy also arrived in Japan yesterday?
     * 是的，比利昨天早上已经抵达日本了。	Yes, Billy had arrived in Japan yesterday morning.
     * 今天早上你已经打电话给他们了吗？	Had you called them this morning?
     * 是的，今天早上11点我已经打给他们了。	Yes, I had called them at eleven o’clock this morning.
     * 今天早上他们已经完成他们的工作了吗？	Had they finished their work this morning?
     * 当我打给他们时，他们已经完成了他们的工作。	When I called them , they had finished their work.
     * 在我回家以前，我弟弟已经生病了。	Before I came home , my brother had been sick.
     * 在我回家以前，我弟弟已经生病了。（is的过去分词＝been）	My brother had been sick before I came home.
     * 在你到达这里以前，你妈妈已经生病了。	Before you arrived here, your mother had been sick.
     * 她何时生病的？	When was she sick?
     * 6天之前你妈妈已经生病了。	Your mother had been sick six days ago.
     * 她喝过任何的水吗？	Did she drink any water?
     * 是的，她已经喝了一些水，在你到达这里以前。	Yes, she had drunk some water before you arrived here.
     * 你想要吃一些面包吗？	Do you want to eat some bread?
     * 我现在不饿。	I am not hungry now.
     * 我来这之前，我已经吃了许多东西了	I had eaten many things before I came here. (Before I came here, I had eaten many things.)
     * 昨天当你抵达这里时，我已经持续睡了3小时。	When you arrived here yesterday, I had been sleeping for three hours.
     * 今天早上当你看见我时，我已经持续工作3小时了。	When you saw me this morning, I had been working for three hours.
     * 在你抵达中国之前，（天气）已经持续下雨5小时了。	Before you arrived in China, it had been raining for five hours.
     * 昨天下午你在公园里跑步吗？	Did you run in the park yesterday afternoon?
     * 是的，昨天下午当你看见我时我正在公园里跑步。	Yes, I was running in the park when you saw me yesterday afternoon.
     * 在你看见我之前，我已经持续跑了45分钟。	Before you saw me, I had been running for forty-five minutes.
     * 昨天下午你也在公园里跑步吗？	Did you also run in the park yesterday afternoon?
     * 不，昨天下午我在公园等我妈妈。	No, I waited for my mother at the park yesterday afternoon.
     * 在我看见你之前，我已经持续等待我妈妈2小时了。	Before I saw you, I had been waiting for my mother for two hours.
     * 来这之前，（天气）就已经持续下雨从昨天下午2点开始。（用it代表天气）	Before you came here, it had been raining since two o’clock yesterday afternoon.
     * 你现在正在做什么？	What are you doing now?
     * 我现在正在读历史	I am studying history now.
     * 你已经读历史多久了？	How long have you studied history?
     * 在你打给我之前，我已经持续读历史从下午2点开始。	Before you called me, I had been studying history since two o’clock in the afternoon.
     * 你想要和我去动物园吗？	Do you want to go to the zoo with me?
     * 我今天不想去动物园，因为今天是雨天	I don’t want to go to the zoo today, because today is a rainy day.
     * 在你打给我之前，从早上8点开始（天气）已经持续下了7小时的雨。	Before you called me, it had been raining for seven hours since eight o’clock in the morning.
     * 我不喜欢下雨天，而且动物园里的动物们也不喜欢下雨天	I don’t like rainy days, and the animals in the zoo don’t like rainy days either.
     * 明天将会下雨。	It will rain tomorrow.
     * 明天下午将会下雨。	It will rain tomorrow afternoon.
     * 我将会在今晚9点上床睡觉。	I will go to bed at nine o’clock tonight.
     * 你明天将会来北京吗？	Will you come to Beijing tomorrow?
     * 我下星期将不会去上学。	I will not go to school next week.
     * 你现在正在做什么？	What are you doing now?
     * 我正在计划去拜访（visit）我的老朋友。	I am planning to visit my old friend.
     * 你将要何时拜访你的老朋友呢？	When will you visit your old friend?
     * 我将要在后天拜访我的老朋友。	I will visit my old friend the day after tomorrow.
     * 你的朋友住在哪里呢？	Where does your friend live?
     * 我朋友住在北京	My friend lives in Beijing.
     * 你将会停留在那里多少天呢？	How many days will you stay there?
     * 我将会停留在北京一个星期。	I will stay in Beijing for one week.
     * 你曾经去过北京吗？	Have you ever been to Beijing?
     * 不，我还没有去过北京。	No, I have not been to Beijing.
     * 今年我将要和比利去北京。	I will go to Beijing with Billy this year.
     * 比利非常喜欢北京，因为北京有许多漂亮的建筑物。	Billy likes Beijing very much, because there are many beautiful buildings in Beijing.
     * 约翰明天早上8点那时将正在游泳。	John will be swimming at eight o’clock tomorrow morning.
     * 明天下午那时我将正在看电视。	I will be watching TV tomorrow afternoon.
     * 明天下午玛丽将正在在家睡觉。	　　＝Mary will be sleeping at home tomorrow afternoon.（O）（home当“名词”时）
     * 　　＝Mary will be sleeping home tomorrow afternoon.（O）（home当“地点副词”时）
     * 你明天早上10点将会和我去博物馆吗？	Will you go to the museum with me at ten o’clock tomorrow morning?
     * 不，我明天早上10点将不能和你去博物馆。	No, I will not go to the museum with you at ten o’clock tomorrow morning.
     * 因为我明天早上10点将会正在洗我的车。	Because I will be washing my car at ten o’clock tomorrow morning.
     * 明天下午3点你将会和我去书店吗？	Will you go to the bookstore with me at three o’clock tomorrow afternoon?
     * 不，我明天下午3点将不会和你去书店。	No, I will not go to the bookstore with you at three o’clock tomorrow afternoon.
     * 因为我明天下午3点将会正在拜访我叔叔。	Because I will be visiting my uncle at three o’clock tomorrow afternoon.
     * 明天晚上你将会在家吗？	Will you be (at) home tomorrow evening?
     * 是的，明天晚上我将会在家。	Yes, I will be (at) home tomorrow evening.
     * 你明天晚上7点将会和我去公园散步（walk with me in the park）吗？	Will you walk with me in the park at seven o’clock tomorrow evening?
     * 不，我明天晚上7点将不会和你去公园散步。	No, I will not walk with you in the park at seven o’clock tomorrow evening.
     * 因为我明天晚上7点将正在学习英文。	Because I will be learning English at seven o’clock tomorrow evening
     * 今天下午2点之前 她将已经完成她的工作。	She will have finished her work by two o’clock this afternoon.
     * 他明天将会已经看完这本书。	He will have read this book tomorrow.
     * 在5点之前那趟火车将会已经离开。	That train will have left by five o’clock.
     * 在你抵达这里之前我将会已经把你的车洗好。	I will have washed your car before you arrive here.
     * 你已经完成你的功课了吗？	Have you finished your homework?
     * 不，我还没有完成我的功课。	No, I have not finished my homework.
     * 你何时将会已经完成你的功课？	When will you have finished your homework?
     * 我将会已经完成我的功课在5点之前	I will have finished my homework by five o’clock.
     * 你确定吗？	Are you sure?
     * 是的，我确定。	Yes, I am sure.
     * 你已经打扫完你的卧室了吗？	Have you cleaned your bedroom?
     * 没，我还没打扫完我的卧室。	No, I have not cleaned my bedroom.
     * 你何时将会已经打扫完你的卧室？	When will you have cleaned your bedroom?
     * 我将会已经打扫完我的卧室在我老公回家之前。	I will have cleaned my bedroom before my husband come home.
     * 在5点之前，我将已经持续等你3小时了。	I will have been waiting for you for three hours by five o’clock.
     * 当我们到达时，他将已经持续等我们2小时了。	He will have been waiting for us for two hours when we arrive there.
     * 到下个月，我祖父将已经持续住在这里10年了。	My grandfather will have been living here for ten years next　　 month.
     * 你已经开多久了？（drive的过去分词＝driven）	How long have you driven?
     * 在8点之前，我将已经持续开了4小时。	I will have been driving for four hours by eight o’clock.
     * 我儿子已经睡了多久？（sleep的过去分词＝slept）	How long has my son slept?
     * 在8点之前他将已经持续睡了3小时。	He will have been sleeping for three hours by eight o’clock.
     * 你们现在在哪里？	Where are you now?
     * 我们在高速公路上，因为我们将要去北京拜访安妮	We are on the freeway because we will go to Beijing to visit Anne.
     * 安妮已经在北京住多久了？（live的过去分词＝lived）	How long has Anne lived in Beijing?
     * 到下个月，她将已经持续在北京住10年了。（live的过去分词＝lived）	She will have been living in Beijing for ten years next month.
     * 她现在正在等你们吗？	Is she waiting for you now?
     * 是的，她现在正在等我们。	Yes, she is waiting for us now.
     * 当我们到达她的房子时，她将已经持续等我们3小时了。	She will have been waiting for us for three hours when we arrive at her house.
     * 虽然今天我生病了，但我可以上学。	Although I am sick today, I can go to school.
     * 安妮可以说两种语言。	Anne can speak two languages.
     * 今天我不能上学，因为我生病了。	I can not go to school today because I am sick.
     * 我可以出去吗？	May I go out?
     * 你一定是累了。	You must be tired.
     * 为什么他不能去动物园？	Why can’t he go to the zoo?
     * 他不能去动物园因为他生病了。	He can’t go to the zoo because he is sick.
     * 我可以和约翰去图书馆（library）吗？	Can I go to the library with John?
     * 不，你不可以和约翰去图书馆。	No, you can’t go to the library with John.
     * 为什么我不能和约翰去图书馆？	Why can’t I go to the library with John?
     * 因为你还没有打扫客厅。 （clean的过去分词＝cleaned）	Because you haven’t (have not) cleaned the living room.
     * 你和约翰去图书馆之前，你必须打扫客厅。	Before you go to the library with John, you must clean the living room.
     * 我可以问你一个问题吗？	May I ask you a question?
     * 是的，你可以（may）。	Yes, you may.
     * 你喜欢我的新收音机（radio）吗？	Do you like my new radio?
     * 是的，我非常喜欢（like it）。	Yes, I like it very much.
     * 如果现在你允许我和约翰去图书馆，我可以借你那台新收音机。	If you allow me to go to the library with John now, I can lend you the new radio.
     * 如果你可以在6点之前回家，我可以接受你的请求。	If you can go home by six o’clock, I can accept your request.
     * 如果我有一台计算机，我将会非常开心。	If I had a computer,I would be very happy.
     * 如果我有一台电脑,我将会很快乐。	If I had a computer, I would be very happy.
     * 如果你是一只蝴蝶，你就可以飞翔了。	If you were a butterfly, you could fly.
     * 如果你是生病的，你应该要告诉你的老师。	If you were sick, you should tell your teacher.
     * 如果我是富有的，我将会买给我妈妈一栋房子。	If I were rich, I would buy my mother a house.（O）
     * If I were rich, I would buy a house for my mother.（O）
     * 为什么你很难过？	Why are you very sad?
     * 因为我的生日礼物不是一辆自行车。	Because my birthday gift is not a bicycle.
     * 如果我的生日礼物是一辆自行车，我将会非常开心。	If my birthday gift were a bicycle, I would be very happy.
     * 如果我有许多钱，我应该会买一辆自行车。	If I had much money, I should buy a bicycle.
     * 如果我是你爸爸，我应该会买一辆自行车给你。	If I were your father, I should buy you a bicycle.( I should buy a bicycle for you.)
     * 你看见我的狗了吗？	Do you see my dog?
     * 不，我没有。	No, I don’t.
     * 为什么你正在寻找它？ （look for的现在进行式＝looking for）	Why are you looking fot it?
     * 因为它生病了。	Because it is sick.
     * 如果我看见它，我将会告诉（tell）你。（see的过去式＝saw）	If I saw it, I would tell you.
     * 这台电脑是被美国制造的。	This computer is made in America.
     * 这台电脑是（被）中国制造。	This computer is made in China.
     * 这个蛋糕是我妈妈做（制造）的。	This cake is made by my mother.
     * 上星期我们被他教数学。	We were taught math by him last week.
     * 我的书已经被偷了。	My book has been stolen.
     * 你将会被我的老师处罚。	You will be punished by my teacher.
     * 这台电脑是你的生日礼物吗？	Is this computer your birthday gift?
     * 是的，它是我的生日礼物。	Yes, it is my birthday gift.
     * 它是（被）韩国制造的吗？	Is it made in Korea?
     * 不，它是（被）中国制造的。	No, it is made in China.
     * 你的电脑在哪里？	Where is your computer?
     * 我的电脑被偷了。	My computer is stolen.
     * 你的电脑何时被偷了？	When is your computer stolen?
     * 今天早上我的电脑已经在图书馆被偷了。	My computer had been stolen in the library this morning.
     * 如果我是你，我将会告诉我们的老师。	If I were you, I would tell our teacher.
     * 我已经告诉我们的老师了。 （tell的过去分词＝told）	I have told our teacher.
     * 我将会被我爸爸处罚因为那台电脑是他的生日礼物。	I will be punished by my father because that computer is his birthday gift.
     * 如果我是你爸爸，我应该会原谅你。	If I were your father, I should forgive you.
     * 他就是以前教我英文的那个老师。	　　＝He is the teacher who taught me English before. （O）
     * 　　＝He is the teacher that taught me English before. （O）
     * 它是昨天咬我的那只狗。	　　＝It is the dog which bit me yesterday.（O）
     * 　　＝It is the dog that bit me yesterday.（O）
     * 她是想要认识你的那个女孩。	＝This is the girl who wants to know you. （O）
     * 　　＝This is the girl that wants to know you.（O）
     * 她是你想认识的那个女孩。	　＝This is the girl whom you want to know.（O）
     * 　　＝This is the girl that you want to know.（O）
     * 他就是名字是比利的那位学生。	He is the student whose name is Billy.
     * 这是谁的书呢？	Whose book is this?
     * 这本书是我的	This book is mine.
     * 它就是非常贵的那本书。	It is the book which (that) is very expensive.
     * 这本书很好吗？	Is this book very good?
     * 是的，它非常好。	Yes, it is very good.
     * 我朋友应该也喜欢这本书。	My friend should also like this book.
     * 你的朋友是谁？	Who is your friend?
     * 我的朋友就是教我英文的那个女孩。	My friend is the girl who (that) teaches me English.
     * 她的名字是安妮吗？	Is her name Anne?
     * 是的，她就是名字是安妮的那个女孩。	Yes, she is the girl whose name is Anne.
     * 我喜欢游泳。	I like swimming.
     * 我喜欢阅读。	I like reading.
     * 我正在阅读	I am reading.
     * 我喜欢跑步。	I like running.
     * 我正在跑步。	I am running.
     * 她喜欢游泳。	She likes swimming.
     * 她正在游泳。	She is swimming.
     * 他喜欢在教室睡觉。	He likes sleeping in the classroom.
     * 他正在教室睡觉。	He is sleeping in the classroom.
     * 我叔叔喜欢钓鱼	My uncle likes fishing.
     * 我叔叔正在钓鱼。	My uncle is fishing.
     * 我喜欢滑雪	I like skiing.
     * 他喜欢跑步。 （动名词）	He likes running.
     * 他喜欢跑步。 （不定式）	He likes to run.
     * 她喜欢做饭。 （动名词）	She likes cooking.
     * 她喜欢做饭。 （不定式）	She likes to cook.
     * 你父亲喜欢抽烟。 （动名词）	Your father likes smoking.
     * 你父亲喜欢抽烟。 （不定式）	Your father likes to smoke.
     * 钓鱼是我的爱好。	Fishing is my hobby
     * To fish is my hobby
     * 做饭是有趣的。	　　＝Cooking is interesting. （O） （动名词用法）
     * 　　＝To cook is interesting. （O） （不定式用法）
     * 　　＝It is interesting. 的概念
     * 游泳是一个好的运动（动名词）	Swimming is a good exercise.
     * 游泳是一个好的运动 （不定式）	To swim is a good exercise.
     *
     *
     * @return
     */
    public static  <T> void writeStyleExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> clazz) {
        //表头策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //背景浅灰
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)14);
        headWriteCellStyle.setWriteFont(headWriteFont);

        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        OutputStream outputStream = getOutputStream(response, fileName);
        EasyExcel.write(outputStream, clazz).registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(list);

    }


    /**
     * 导出Excel(动态表头)
     * write时不传入class,table时传入并设置needHead为false
     * @return
     */
    public static  <T> void writeDynamicHeadExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> clazz, List<List<String>> headList) {

        OutputStream outputStream = getOutputStream(response, fileName);

        EasyExcel.write(outputStream)
                .head(headList)
                .sheet(sheetName)
                .table().head(clazz).needHead(Boolean.FALSE)
                .doWrite(list);
    }


    /**
     * 导出时生成OutputStream
     */
    private static OutputStream getOutputStream(HttpServletResponse response, String fileName) {
        //创建本地文件
        String filePath = fileName + ".xlsx";
        File file = new File(filePath);
        try {
//            if (!file.exists() || file.isDirectory()) {
//                file.createNewFile();
//            }
            fileName = new String(filePath.getBytes(), "ISO-8859-1");
            response.addHeader("Content-Disposition", "filename=" + fileName);
            return response.getOutputStream();
        } catch (IOException  e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 返回ExcelReader
     * @param excel         文件
     * @param clazz         实体类
     * @param excelListener
     */
    private static <T> ExcelReader getReader(MultipartFile excel, Class<T> clazz, EasyExcelListener excelListener) {
        String filename = excel.getOriginalFilename();

        try {
            if (filename == null || (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
                return null;
            }

            InputStream inputStream = new BufferedInputStream(excel.getInputStream());

            ExcelReader excelReader = EasyExcel.read(inputStream, clazz, excelListener).build();

            inputStream.close();

            return excelReader;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static class ImportDataListener extends AnalysisEventListener {

        private IEasyExcelImportService easyExcelImportService;
        private EasyExcelImportResult returnResult;
        private Map<String , Object> otherParamsMap ;
        private AsyncTaskExecutor asyncTaskExecutor;
        private Integer headRowNum;

        public ImportDataListener(IEasyExcelImportService easyExcelImportService , EasyExcelImportResult returnResult , Map<String , Object> otherParamsMap , AsyncTaskExecutor asyncTaskExecutor , Integer headRowNum){
            this.easyExcelImportService = easyExcelImportService;
            this.returnResult = returnResult;
            this.otherParamsMap = otherParamsMap;
            this.asyncTaskExecutor = asyncTaskExecutor;
            this.headRowNum = headRowNum;
        }
        private List<Object> importModelList = new ArrayList<>();
        private AtomicInteger total = new AtomicInteger(0);
        private AtomicInteger success = new AtomicInteger(0);
        private List<Future> taskFutureList = new ArrayList<>();
        @Override
        public void invoke(Object easyExcelImportModel, AnalysisContext analysisContext) {
//            logger.info("Import For : {} Start" , easyExcelImportModel.getClass());
            Integer num = analysisContext.readRowHolder().getRowIndex();
            EasyExcelImportResult easyExcelImportResult = easyExcelImportService.validateModel(easyExcelImportModel , otherParamsMap);
            if(!ObjectUtils.isEmpty(easyExcelImportResult)){
                returnResult.setTotalNum(total.incrementAndGet());
                easyExcelImportResult.setRowNum(num + headRowNum - 1);
                if(easyExcelImportResult.getImportSuccess() && StringUtils.isEmpty(easyExcelImportResult.getErrorMessage())){
                    returnResult.setSuccessNum(success.incrementAndGet());
                    importModelList.add(easyExcelImportModel);
                }else{
                    returnResult.addFailedImport(easyExcelImportResult);
                }

//                this.returnResult = easyExcelImportResult;

                /*if(importModelList.size() >= BATCH_IMPORT_SIZE){
//                    easyExcelImportService.saveImportModelList(importModelList);
                    List<Object> threadImportModelList = new ArrayList<>();
                    threadImportModelList.addAll(importModelList);
                    taskFutureList.add(asyncTaskExecutor.submit(() -> {
                        easyExcelImportService.saveImportModelList(threadImportModelList);
                    }));
                    importModelList.clear();
                }*/
            }

        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            if(!ObjectUtils.isEmpty(returnResult.getFailedList())){
                returnResult.setImportSuccess(false);
                easyExcelImportService.clearCachedData();
            }else{
                if(!ObjectUtils.isEmpty(importModelList)){
                    Integer saveTimes = importModelList.size() % BATCH_IMPORT_SIZE == 0 ? importModelList.size() / BATCH_IMPORT_SIZE : (importModelList.size() / BATCH_IMPORT_SIZE + 1);
                    CountDownLatch cdl = new CountDownLatch(saveTimes);
                    AtomicInteger sucessCount = new AtomicInteger();
                    for(Integer i = 0 ; i < saveTimes ; i++){
                        Integer endSize = (i == saveTimes - 1 ? importModelList.size() : (i + 1) * BATCH_IMPORT_SIZE);
                        AtomicInteger ind = new AtomicInteger(i);
                        asyncTaskExecutor.execute(() -> {
                            Integer importCount = 0;
                            List<Object> subImportList = new ArrayList<>();
                            try {
                                subImportList = importModelList.subList(ind.get() * BATCH_IMPORT_SIZE , endSize);
                                importCount = easyExcelImportService.saveImportModelList(subImportList , otherParamsMap);
                            } catch (Exception e) {
                                logger.error(ExceptionUtils.getFullStackTrace(e));
                                subImportList.clear();
                                cdl.countDown();
                                return;
                            }
                            sucessCount.addAndGet(importCount);
//                            subImportList.clear();
                            cdl.countDown();
                        });
                    }

                    try {
                        cdl.await();
                    } catch (InterruptedException e) {
                        logger.error(ExceptionUtils.getFullStackTrace(e));
                    }

                    importModelList.clear();
                    returnResult.setImportSuccess(true);
                    returnResult.setSuccessNum(sucessCount.get());
                    /*if(!ObjectUtils.isEmpty(taskFutureList)){
                    taskFutureList.forEach(future -> {
                        try {
                            future.get();
                        } catch (InterruptedException e) {
                            logger.error(ExceptionUtils.getFullStackTrace(e));
                        }  (ExecutionException e) {
                            logger.error(ExceptionUtils.getFullStackTrace(e));
                        }
                    });

                    taskFutureList.clear();
                }*/
//                    easyExcelImportService.saveImportModelList(importModelList);
//                    importModelList.clear();
                }
            }

        }
    }
}
